package com.jiuyi.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.jiuyi.common.JacksonObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.service.Contact;

import java.util.ArrayList;
import java.util.List;

/**
 *   DATA: 2022/10/17
 *   Author: 晨夕
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class WebMvcConfig extends WebMvcConfigurationSupport {


    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:META-INF/resources/webjars/");
        registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
        registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
    }

    /**
     * 扩展mvc框架的消息转换器
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters){
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器，底层使用Jackson将Java对象转为json
        mappingJackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());
        //将上面的消息转换器对象追加到mvc框架的转换器集合中
        converters.add(0,mappingJackson2HttpMessageConverter);
    }

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //enable : 是否启用 Swagger
//                .enable(false)
                .select()
                //RequestHandlerSelectors : 配置要扫描接口的方式
                //basePackage : 指定要扫描的包
                //any() : 扫描全部
                //none() : 不扫描
                //withClassAnnotation : 扫描类上的注解，参数是一个注解的方射对象
                //withMethodAnnotation : 扫描方法上的注解，参数是一个注解的方射对象
                .apis(RequestHandlerSelectors.basePackage("com.jiuyi.controller"))
                //paths() : 过滤路径
//                .paths(PathSelectors.ant("/test/**")) //不是 /test/**的无法访问
                .paths(PathSelectors.any())
                .build();

    }
    //配置 Swagger 信息=apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("晨夕","120.25.75.85:8080","3397085462@qq.com");

        return new ApiInfoBuilder()
                .contact(contact)
                .title("好味来餐厅")
                .version("1.0")
                .description("好味来餐厅接口文档")
                .build();
    }
}
